package com.diandian.backend.log.service;

import com.diandian.backend.log.domain.LogRecord;
import com.diandian.backend.log.dto.*;
import com.diandian.backend.log.mapper.LogRecordMapper;
import com.diandian.backend.log.resp.LogRecordPageResp;
import com.diandian.base.boot.starter.response.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LogRecordService {

    @Autowired
    private LogRecordMapper logRecordMapper;

    public PageResult page(LogRecordPageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = logRecordMapper.getCount(requestDTO);
        if (totalCount == 0) {
            return new PageResult(pageNum, pageSize, 0);
        }

        // 查询数据集合
        List<LogRecord> dataList = logRecordMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<LogRecordPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                LogRecordPageResp item = new LogRecordPageResp();
                item.setId(x.getId());
                item.setClientId(x.getClientId());
                item.setBusinessType(x.getBusinessType());
                item.setBusinessId(x.getBusinessId());
                item.setContent(x.getContent());
                item.setLevel(x.getLevel());
                item.setCreateTime(x.getCreateTime());
                item.setDel(x.getDel());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public void add(LogRecordAddParamDTO param) {
        param.setCreateTime(new Date());
        param.setDel(0);
        logRecordMapper.add(param);
    }
}
